package com.ghsc.ghxc.jsb.ui.live.view;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alivc.auicommon.common.base.util.CommonUtil;
import com.alivc.auicommon.common.roombase.Const;
import com.alivc.auicommon.core.base.Actions;
import com.alivc.auicommon.core.base.LimitSizeRecyclerView;
import com.alivc.auicommon.core.base.MessageModel;
import com.alivc.auicommon.core.utils.MessageHelper;
import com.alivc.auimessage.model.base.AUIMessageModel;
import com.alivc.auimessage.model.message.JoinGroupMessage;
import com.alivc.auimessage.model.message.LeaveGroupMessage;
import com.alivc.auimessage.model.message.MuteGroupMessage;
import com.alivc.auimessage.model.message.UnMuteGroupMessage;
import com.aliyun.aliinteraction.model.CancelMuteUserModel;
import com.aliyun.aliinteraction.model.Message;
import com.aliyun.aliinteraction.model.MuteUserModel;
import com.aliyun.aliinteraction.roompaas.message.listener.SimpleOnMessageListener;
import com.aliyun.aliinteraction.roompaas.message.model.CommentModel;
import com.aliyun.aliinteraction.roompaas.message.model.CustAdminModel;
import com.aliyun.aliinteraction.roompaas.message.model.CusuBuyGoodsModel;
import com.aliyun.aliinteraction.roompaas.message.model.CusuLiveBlackModel;
import com.aliyun.aliinteraction.roompaas.message.model.CusuLiveGoodsModel;
import com.aliyun.aliinteraction.roompaas.message.model.CusuLiveGoodsRefreshModel;
import com.aliyun.aliinteraction.roompaas.message.model.CusuLiveRedModel;
import com.aliyun.aliinteraction.roompaas.message.model.CusuRenCountModel;
import com.aliyun.aliinteraction.roompaas.message.model.CusuSendMsgModel;
import com.aliyun.aliinteraction.roompaas.message.model.CusuUpMsgModel;
import com.aliyun.aliinteraction.roompaas.message.model.GiftModel;
import com.aliyun.aliinteraction.roompaas.message.model.JoinLinkMicModel;
import com.aliyun.aliinteraction.roompaas.message.model.LeaveLinkMicModel;
import com.aliyun.aliinteraction.roompaas.message.model.StartLiveModel;
import com.aliyun.aliinteraction.roompaas.message.model.StopLiveModel;
import com.aliyun.aliinteraction.uikit.core.AppConfig;
import com.aliyun.aliinteraction.uikit.core.BaseComponent;
import com.aliyun.aliinteraction.uikit.core.ComponentHolder;
import com.aliyun.aliinteraction.uikit.core.IComponent;
import com.aliyun.aliinteraction.uikit.core.LiveConst;
import com.aliyun.aliinteraction.uikit.uibase.helper.RecyclerViewHelper;
import com.aliyun.aliinteraction.uikit.uibase.util.AppUtil;
import com.aliyun.auibarrage.FlyView;
import com.aliyun.auipusher.LiveContext;
import com.cn.appcore.event.RxBus;
import com.cn.appcore.event.RxBusBean;
import com.ghsc.ghxc.jsb.R;
import com.ghsc.ghxc.jsb.config.CustSignKey;
import com.ghsc.ghxc.jsb.config.UserManager;
import com.ghsc.ghxc.jsb.ui.live.bean.LiveMessageDataBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author puke
 * 信息
 * @version 2021/7/29
 */
public class LiveMessageView extends RelativeLayout implements ComponentHolder {

    private static final String TAG = LiveMessageView.class.getSimpleName();
    private static final int NICK_SHOW_MAX_LENGTH = 15;
    private static final String WELCOME_TEXT = "欢迎来到直播间，直播内容和评论禁止政治、低俗色情、吸烟酗酒或发布虚假信息等内容，若有违反将禁播、封停账号。";

    protected final FlyView flyView;
    protected final LimitSizeRecyclerView recyclerView;

    private final MessageHelper<MessageModel> messageHelper;
    private final Component component = new Component();
    private final LinearLayoutManager layoutManager;
    private final RecyclerViewHelper<MessageModel> recyclerViewHelper;
    private final int commentMaxHeight = AppUtil.getScreenHeight() / 5;
    private final Runnable refreshUITask = new Runnable() {
        @Override
        public void run() {
            recyclerView.invalidate();
        }
    };

    private int lastPosition;
    private boolean forceHover;

    public LiveMessageView(Context context) {
        this(context, null, 0);
    }

    public LiveMessageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }






    public LiveMessageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        View.inflate(context, R.layout.ilr_view_live_message, this);
        flyView = findViewById(R.id.message_fly_view);
        recyclerView = findViewById(R.id.message_recycler_view);

        // 弹幕面板
        recyclerView.setMaxHeight(commentMaxHeight);
        layoutManager = new LinearLayoutManager(context);
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerViewHelper = RecyclerViewHelper.of(recyclerView, R.layout.ilr_item_message,
                new RecyclerViewHelper.HolderRenderer<MessageModel>() {
                    @Override
                    public void render(RecyclerViewHelper<MessageModel> viewHelper, RecyclerViewHelper.ViewHolder holder, final MessageModel model, int position, int itemCount, List<Object> payloads) {
                        TextView content = holder.getView(R.id.item_content);
                        content.setMovementMethod(new LinkMovementMethod());
                        if (model.type==30001)
                            content.setTextColor(ContextCompat.getColor(context, R.color.main_color));
                        else
                            content.setTextColor(model.contentColor);
                        if (TextUtils.isEmpty(model.userNick)) {
                            content.setText(model.content);
                        } else {
                            String prefix = model.userNick + "：";
                            String postfix = model.content;

                            SpannableString spannableString = new SpannableString(prefix + postfix);
                            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context,  model.userId.equals(component.getAnchorId()) ? R.color.im_color_z: R.color.im_color)), 0,
                                    prefix.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


                            ClickableSpan clickableSpan = new ClickableSpan() {
                                @Override
                                public void onClick(@NonNull View widget) {
                                    if (model.userId.equals(component.getAnchorId()) ){
                                        //如果是主播
                                        return;
                                    }
                                    if (TextUtils.equals(model.userId, Const.getUserId())) {
                                        //如果是自己
                                        return;
                                    }
                                    RxBus.getDefault().post(
                                            new RxBusBean(
                                                    CustSignKey.CUST_LIVE_MESSAGE,
                                                    CustSignKey.CUST_LIVE_MESSAGE,new LiveMessageDataBean(model,component.getAnchorId()))
                                    );
//                                    component.handleUserClick(model,component.getAnchorId());
                                }

                                @Override
                                public void updateDrawState(@NonNull TextPaint ds) {
                                    ds.setUnderlineText(false);
                                    ds.setColor(ContextCompat.getColor(context, R.color.im_color_z ));
                                }
                            };
                            spannableString.setSpan(clickableSpan,
                                    0, prefix.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                            if (component.isOwner()) {
//                                ClickableSpan clickableSpan = new ClickableSpan() {
//                                    @Override
//                                    public void onClick(@NonNull View widget) {
//                                        component.handleUserClick(model);
//                                    }
//
//                                    @Override
//                                    public void updateDrawState(@NonNull TextPaint ds) {
//                                        ds.setUnderlineText(false);
//                                        ds.setColor(ContextCompat.getColor(context, R.color.im_color_z ));
//                                    }
//                                };
//                                spannableString.setSpan(clickableSpan,
//                                        0, prefix.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                            }
                            content.setText(spannableString);
                        }
                        content.setOnLongClickListener(new OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                if (!TextUtils.isEmpty(model.userId)) {
                                    // do something
                                }
                                return true;
                            }
                        });

                        content.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (!TextUtils.isEmpty(model.userId)) {
                                    // do something
                                }
                            }
                        });
                    }
                });

        // 维度消息控制逻辑
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                refreshUnreadTips();
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                forceHover = false;
                refreshUnreadTips();
            }
        });

        // 消息控制辅助类
        messageHelper = new MessageHelper<MessageModel>()
                .setCallback(new MessageHelper.Callback<MessageModel>() {
                    @Override
                    public int getTotalSize() {
                        return recyclerViewHelper.getItemCount();
                    }
                    @Override
                    public void onMessageAdded(MessageModel message) {
                        Log.e("文本消息 消息控制辅助类", "addMessageToPanel: userId="+ message.userId+"userNick="+message.userNick+"content="+message.content );
                        addMessageToPanel(Collections.singletonList(message));
                    }

                    @Override
                    public void onMessageRemoved(int suggestRemoveCount) {
                        lastPosition -= suggestRemoveCount;
                        if (forceHover) {
                            postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    forceHover = true;
                                }
                            }, 10);
                        }
                        recyclerViewHelper.removeDataWithoutAnimation(0, suggestRemoveCount);
                    }
                });

        MessageModel model = new MessageModel(null, WELCOME_TEXT);
        model.contentColor = Color.parseColor("#93DDEF");

        Log.e("文本消息", "addMessageToPanel: "+ model.toString());
        addMessageToPanel(Collections.singletonList(model));
    }

    /**
     * @return 是否开启未读提示条逻辑
     */
    protected boolean enableUnreadTipsLogic() {
        return true;
    }

    /**
     * @return 是否开启系统消息显示逻辑
     */
    protected boolean enableSystemLogic() {
        return true;
    }

    private void refreshUnreadTips() {
        if (!enableUnreadTipsLogic()) {
            // 未开启未读提示条逻辑时, 不做额外处理
            return;
        }

        int itemCount = recyclerViewHelper.getItemCount();
        int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        if (lastPosition >= itemCount) {
            lastPosition = lastVisibleItemPosition;
        } else {
            lastPosition = Math.max(lastVisibleItemPosition, lastPosition);
        }

        if (forceHover || (lastPosition >= 0 && lastPosition < itemCount - 1)) {
            // 一旦悬停, 就要等到列表滚动后, 才能解除悬停状态
            forceHover = true;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        final int maxMessageHeight;
        if (component.isLandscape()) {
            // 横屏
            maxMessageHeight = AppUtil.getScreenHeight() / 3;
            // 宽度为屏幕一半
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                    AppUtil.getScreenWidth() / 2, MeasureSpec.getMode(widthMeasureSpec)
            );
        } else {
            // 竖屏
            int systemMaxHeight = enableSystemLogic() ? (getResources().getDimensionPixelOffset(R.dimen.live_message_fly_height) + getResources().getDimensionPixelOffset(R.dimen.message_fly_bottom_margin)) : 0;
            maxMessageHeight = commentMaxHeight + systemMaxHeight;
        }
        if (height > maxMessageHeight) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                    maxMessageHeight, MeasureSpec.getMode(heightMeasureSpec)
            );
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    protected void addSystemMessage(CharSequence content) {
        FlyView.FlyItem item = new FlyView.FlyItem();
        item.content = content;
        addSystemMessage(item);
    }

    protected void addSystemMessage(FlyView.FlyItem item) {
        if (enableSystemLogic()) {
            // 开启系统消息显示逻辑, 才做处理
            flyView.addItem(item);
        }
    }

    protected void addMessage(String type, String content) {
        addMessage(new MessageModel(type, content));
    }

    protected void addMessage(MessageModel messageModel) {
        Log.e(TAG, "addMessage:1111 我是 "+ messageModel.userId);
        messageHelper.addMessage(messageModel);
    }

    protected void addMessageByUserId(String userId, String type, String content) {
        Log.e(TAG, "addMessage:2222 我是 "+userId);
        messageHelper.addMessage(new MessageModel(userId, type, content));
    }

    /**
     * 弹幕信息添加到面板
     *
     * @param addedList 弹幕信息
     */
    public void addMessageToPanel(final List<MessageModel> addedList) {
        boolean isLastCompletelyVisible = layoutManager.findLastVisibleItemPosition()
                == recyclerViewHelper.getItemCount() - 1;
        recyclerViewHelper.addData(addedList);
        if (!forceHover && isLastCompletelyVisible) {
            Log.e("消息弹幕=====", "擦1111: " );
            // 已触底时, 随消息联动
            layoutManager.scrollToPositionWithOffset(
                    recyclerViewHelper.getItemCount() - 1, Integer.MIN_VALUE);
            postDelayed(refreshUITask, 100);
            lastPosition = 0;
        } else {
            Log.e("消息弹幕=====", "擦22222: " );
            refreshUnreadTips();
        }
    }


    public void addMessageToPanel2(final List<MessageModel> addedList) {
        recyclerViewHelper.addData(addedList);
        // 已触底时, 随消息联动
        layoutManager.scrollToPositionWithOffset(
                recyclerViewHelper.getItemCount() - 1, Integer.MIN_VALUE);
        postDelayed(refreshUITask, 100);
        lastPosition = 0;
    }


    /**
     * @return 首条系统消息 (返回null时, 不展示)
     */
    @Nullable
    protected MessageModel getSystemAlertMessageModel() {
        MessageModel systemMessage = new MessageModel(
                LiveConst.SYSTEM_NOTICE_NICKNAME, LiveConst.SYSTEM_NOTICE_ALERT);
        systemMessage.contentColor = Color.parseColor("#12DBE6");
        return systemMessage;
    }

    @Override
    public IComponent getComponent() {
        return component;
    }

    protected static String truncateNick(String nick) {
        if (!TextUtils.isEmpty(nick) && nick.length() > NICK_SHOW_MAX_LENGTH) {
            nick = nick.substring(0, NICK_SHOW_MAX_LENGTH);
        }
        return nick;
    }

    private class Component extends BaseComponent {
        @Override
        public void onInit(LiveContext liveContext) {
            super.onInit(liveContext);

            // 回放不展示信息面板
            setVisibility(VISIBLE);

            // 监听房间事件
//            roomChannel.addEventHandler(new SampleRoomEventHandler() {
//                @Override
//                public void onEnterOrLeaveRoom(RoomInOutEvent event) {
//                    LiveMessageView.this.onEnterOrLeaveRoom(event);
//                }
//
//                @Override
//                public void onRoomUserKicked(KickUserEvent event) {
////                    if (!TextUtils.equals(roomChannel.getUserId(), event.kickUser)) {
////                        // 其他人, 移除列表, 面板提示
////                        addSystemMessage(String.format("%s被主播移除房间", truncateNick(event.kickUserName)));
////                    }
//                }
//            });

            getMessageService().addMessageListener(new SimpleOnMessageListener() {
                @Override
                public void onJoinLinkMic(AUIMessageModel<JoinLinkMicModel> message) {
                    super.onJoinLinkMic(message);
                    Log.e(TAG, "麦麦=onJoinLinkMic: "+message.senderInfo.userId );
                    RxBus.getDefault().post(
                            new RxBusBean(
                                    CustSignKey.CUST_LIVE_MAI_ADD,
                                    CustSignKey.CUST_LIVE_MAI_ADD,message.senderInfo.userId )
                    );
                }
                @Override
                public void onLeaveLinkMic(AUIMessageModel<LeaveLinkMicModel> message) {
                    super.onLeaveLinkMic(message);
                    Log.e(TAG, "麦麦=onLeaveLinkMic: "+message.senderInfo.userId );
                    RxBus.getDefault().post(
                            new RxBusBean(
                                    CustSignKey.CUST_LIVE_MAI_REDUCE,
                                    CustSignKey.CUST_LIVE_MAI_REDUCE,message.senderInfo.userId )
                    );
                }
                @Override
                public void onCommentReceived(AUIMessageModel<CommentModel> message) {
                    String senderId = message.senderInfo.userId;
                    Log.e("AUIMessageUserInfo", "onCommentReceived: "+ message.senderInfo.userId +"useid="+message.senderInfo.userNick +"当前登录的="+Const.getUserId());
                    if (AppConfig.INSTANCE.showSelfCommentFromLocal()
                            &&  TextUtils.equals(senderId, Const.getUserId())) {
                        // 自己发送的消息不做上屏显示
                        return;
                    }

                    boolean containsKey = LiveBlackList.INSTANCE.getLiveBlackSetList().contains(senderId);
                    String nick = truncateNick(message.senderInfo.userNick);
                    if (containsKey) {
                        addMessageByUserId(senderId, nick, "******");
                    }else{
                        addMessageByUserId(senderId, nick, message.data.content);
                    }
                }

                @Override
                public void onStopLive(AUIMessageModel<StopLiveModel> message) {
                    if (!isOwner()) {
                        addSystemMessage("直播已结束");
                    }
                }

                @Override
                public void onStartLive(AUIMessageModel<StartLiveModel> message) {
                    if (!isOwner()) {
                        addSystemMessage("直播已开始");
                    }
                }

                @Override
                public void onJoinGroup(AUIMessageModel<JoinGroupMessage> message) {
                    String userNick = message.senderInfo.userNick;
                    String userAvatar = message.senderInfo.userAvatar;
                    Log.e("加入了", "onJoinGroup: "+userNick+"=="+userAvatar);
                    if (!TextUtils.isEmpty(userNick)) {
                        // addSystemMessage(truncateNick(userNick) + "加入了直播间");
                    }
                }

                @Override
                public void onLeaveGroup(AUIMessageModel<LeaveGroupMessage> message) {
                    String userNick = message.senderInfo.userNick;
                    if (!TextUtils.isEmpty(userNick)) {
                        // addSystemMessage(truncateNick(userNick) + "离开了直播间");
                    }
                }

                @Override
                public void onMuteGroup(AUIMessageModel<MuteGroupMessage> message) {
                    addSystemMessage("主播开启了全体禁言");
                }

                @Override
                public void onUnMuteGroup(AUIMessageModel<UnMuteGroupMessage> message) {
                    super.onUnMuteGroup(message);
                    addSystemMessage("主播取消了全体禁言");
                }

                @Override
                public void onMuteUser(Message<MuteUserModel> message) {
                    boolean isSelf = TextUtils.equals(getUserId(), message.data.userId);
                    String subject = isSelf ? "您" : truncateNick(message.data.userNick);
                    addSystemMessage(String.format("%s被主播禁言了", subject));

                }

                @Override
                public void onCancelMuteUser(Message<CancelMuteUserModel> message) {
                    boolean isSelf = TextUtils.equals(getUserId(), message.data.userId);
                    String subject = isSelf ? "您" : truncateNick(message.data.userNick);
                    addSystemMessage(String.format("%s被主播取消禁言了", subject));

                }
                @Override
                public void onGiftReceived(AUIMessageModel<GiftModel> message) {
                    super.onGiftReceived(message);
                    Log.e(TAG, "礼物消息3001: "+message.data.content);
                    addMessageToPanel(Collections.singletonList(new MessageModel(
                            message.senderInfo.userId,
                            message.data.userName,
                            message.data.content,CustSignKey.LIVE_IM_GIF
                    )));

                }

                @Override
                public void onCustAdminReceived(AUIMessageModel<CustAdminModel> message) {
                    super.onCustAdminReceived(message);
                    //直播管理员30002
//                    if (message.type==CustSignKey.LIVE_IM_ADMIN){
//                        LiveAdminIdBean bean = UserManager.INSTANCE.getLiveIdAdmin();
//                        if (bean ==null){
//                            if (message.data.isAdmin==0){
//                                return;
//                            }
//                            bean=new LiveAdminIdBean();
//                            bean.getList().add(message.data.liveId);
//                        }else{
//                            if (!bean.getList().contains(message.data.liveId)) {
//                                if (message.data.isAdmin==0){
//                                    return;
//                                }
//                                bean.getList().add(message.data.liveId);
//                            }else{
//                                if (message.data.isAdmin==0){
//                                    bean.getList().remove(message.data.liveId);
//                                }
//                            }
//                        }
//                        if (bean.getList().size()>10) {
//                            bean.getList().remove(0);
//                        }
//                        UserManager.INSTANCE.setLiveIdAdmin(bean);
//                        Log.e(TAG, "onCustAdminReceived 直播管理员3002一共: "+UserManager.INSTANCE.getLiveIdAdmin().getList().size() );
//                        for (int i = 0; i < UserManager.INSTANCE.getLiveIdAdmin().getList().size(); i++) {
//                            Log.e(TAG, "onCustAdminReceived 直播管理员3002: "+UserManager.INSTANCE.getLiveIdAdmin().getList().get(i) );
//                        }
//
//                    }

                }

                @Override
                public void onCustUpMsgReceived(AUIMessageModel<CusuUpMsgModel> message) {
                    super.onCustUpMsgReceived(message);
                    //直播 主播  禁言 30003
                    RxBus.getDefault().post(
                            new RxBusBean(
                                    CustSignKey.CUST_LIVE_MESSAGE_UP,
                                    CustSignKey.CUST_LIVE_MESSAGE_UP,message.data)
                    );
                }

                @Override
                public void onCustLiveRedReceived(AUIMessageModel<CusuLiveRedModel> message) {
                    super.onCustLiveRedReceived(message);
                    //直播 主播 发红包
//                    addMessageToPanel(Collections.singletonList(new MessageModel(
//                            message.senderInfo.userId,
//                            message.senderInfo.userNick,
//                            message.data.content,CustSignKey.LIVE_IM_RED
//                    )));
                    RxBus.getDefault().post(
                            new RxBusBean(
                                    CustSignKey.CUST_LIVE_RED,
                                    CustSignKey.CUST_LIVE_RED,message.data));
                }

                @Override
                public void onCustLiveGoodsReceived(AUIMessageModel<CusuLiveGoodsModel> message) {
                    super.onCustLiveGoodsReceived(message);
                    //直播 主播 发商品
                    RxBus.getDefault().post(
                            new RxBusBean(
                                    CustSignKey.CUST_LIVE_GOODS,
                                    CustSignKey.CUST_LIVE_GOODS,message.data));
                }
                @Override
                public void onCusuLiveGoodsRefreshReceived(AUIMessageModel<CusuLiveGoodsRefreshModel> message) {
                    super.onCusuLiveGoodsRefreshReceived(message);
                    //直播 主播 上架或者下架刷新
                    RxBus.getDefault().post(
                            new RxBusBean(
                                    CustSignKey.CUST_LIVE_GOODS_REFRESH,
                                    CustSignKey.CUST_LIVE_GOODS_REFRESH,message.data));
                }


                @Override
                public void onCusuLiveBlackReceived(AUIMessageModel<CusuLiveBlackModel> message) {
                    super.onCusuLiveBlackReceived(message);
                    //直播 拉黑名单和取消拉黑名单
                    RxBus.getDefault().post(
                            new RxBusBean(
                                    CustSignKey.CUST_LIVE_BLCAK_ADD,
                                    CustSignKey.CUST_LIVE_BLCAK_ADD,message.data));

                }

                @Override
                public void onCusuBuyGoodsModel(AUIMessageModel<CusuBuyGoodsModel> message) {
                    super.onCusuBuyGoodsModel(message);
                    addSystemMessage(message.data.content);
                }

                @Override
                public void onCusuRenCountModel(AUIMessageModel<CusuRenCountModel> message) {
                    super.onCusuRenCountModel(message);
                    //人数
                    LiveBlackList.INSTANCE.setNumberMultiple(Integer.parseInt(message.data.content));
                    RxBus.getDefault().post(
                            new RxBusBean(
                                    CustSignKey.LIVE_IM_REN_COUNT,
                                    CustSignKey.LIVE_IM_REN_COUNT));
                }

                @Override
                public void onCusuSendMsgModel(AUIMessageModel<CusuSendMsgModel> message) {
                    super.onCusuSendMsgModel(message);
                    //自定义虚拟发送消息
//                    Log.e("发送消息队列 接收到==", "onCusuSendMsgModel: "+message.data.msg);
                    List<MessageModel> mList = new ArrayList<MessageModel>();
                    mList.add(new MessageModel( message.data.uid,message.data.nickname,message.data.msg));
                    addMessageToPanel2(mList);
//                    RxBus.getDefault().post(
//                            new RxBusBean(
//                                    CustSignKey.LIVE_IM_SEND_MSG,
//                                    CustSignKey.LIVE_IM_SEND_MSG,message.data));
                }

                @Override
                public void onRawMessageReceived(AUIMessageModel<String> message) {
                    // 监听全部消息, 仅用于开发测试阶段
                    Log.e(TAG, "消息 onRawMessageReceived: "+message.data.toString());
                }
            });
        }

        @Override
        public void onEvent(String action, Object... args) {
            if (Actions.SHOW_MESSAGE.equals(action)) {
                if (!AppConfig.INSTANCE.showSelfCommentFromLocal()) {
                    // 非本地上屏, 不处理
                    return;
                }

                if (args.length >= 1) {
                    MessageModel messageModel = (MessageModel) args[0];
                    // 判断是否忽略弹幕频率限制
                    boolean ignoreFreqLimit = args.length > 1 && Boolean.TRUE.equals(args[1]);
                    if (ignoreFreqLimit) {
                        // 忽略限流控制时, 直接上屏
                        addMessageToPanel(Collections.singletonList(messageModel));
                    } else {
                        // 默认是交给消息流控
                        addMessage(messageModel);
                    }
                } else {
                }
            }
        }

        @Override
        public void onActivityDestroy() {
            if (messageHelper != null) {
                messageHelper.destroy();
            }
        }

        private void handleUserClick(final MessageModel model,String anchorId) {
            String userId = model.userId;
            if (TextUtils.isEmpty(userId)) {
                CommonUtil.showToast(getContext(), "用户ID为空");
                return;
            }
            if (TextUtils.equals(userId, Const.getUserId())) {
                CommonUtil.showToast(getContext(), "不能对自己进行操作");
                return;
            }

//            DialogUtil.doAction(
//                    getContext(), String.format("对%s执行", model.userNick),
//                    new DialogUtil.Action("禁言", new Runnable() {
//                        @Override
//                        public void run() {
//                            ImMuteUserReq req = new ImMuteUserReq();
//                            req.groupId = component.getGroupId();
//                            req.broadCastType = BroadcastType.SPECIFIC.getValue();
//                            req.muteUserList = new ArrayList<String>() {{
//                                add(model.userId);
//                            }};
//                            interactionService.muteUser(req, new ToastCallback<ImMuteUserRsp>("禁言"));
//                        }
//                    }),
//                    new DialogUtil.Action("取消禁言", new Runnable() {
//                        @Override
//                        public void run() {
//                            ImCancelMuteUserReq req = new ImCancelMuteUserReq();
//                            req.groupId = component.getGroupId();
//                            req.broadCastType = BroadcastType.SPECIFIC.getValue();
//                            req.cancelMuteUserList = new ArrayList<String>() {{
//                                add(model.userId);
//                            }};
//                            interactionService.cancelMuteUser(req, new ToastCallback<ImCancelMuteUserRsp>("取消禁言"));
//                        }
//                    })
//            );
        }
    }
}
